package com.ohs.ohsoa.system.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.ohs.ohsoa.system.entity.Subgroup;
import com.ohs.ohsoa.system.entity.SubgroupAndUserRelation;
import com.ohs.ohsoa.system.entity.UserInfo;
import com.ohs.ohsoa.system.service.SubgroupService;
import com.ohs.ohsoa.system.vo.SubgroupRequestBean;
import com.ohs.ohsoa.system.vo.SubgroupUserInfoVo;
import com.ohs.ohsoa.system.vo.SubgroupVo;
import com.ohs.ohsoa.system.vo.TreeVo;
import com.ohs.ohsoa.utils.auth.UserLoginToken;
import com.ohs.ohsoa.utils.bean.requestBean.PageParam;
import com.ohs.ohsoa.utils.bean.responseBean.ResultBean;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * Subgroup小组设置
 *
 * @author mwx
 * @since  2021年9月22日
 */
@Api(value = "小组设置controller", tags = "小组设置")
@RestController
@RequestMapping("/subgroup")
public class SubGroupController {

    @Resource
    private SubgroupService service;

    /**
     * 获取小组列表信息
     *
     * @param pageParam 检索用参数
     * @return 实例对象
     * @author mwx
     * @since 2021年9月22日
     */
    @UserLoginToken
    @PostMapping(value = "/listSubgroup")
    @ApiOperation(httpMethod = "POST", value = "获取小组列表信息")
    public ResultBean<PageInfo<SubgroupVo>> listSubgroup(@RequestBody @ApiParam(name = "") PageParam<Subgroup> pageParam) {
        List<SubgroupVo> listSubgroup = service.listSubgroup(pageParam);
        return ResultBean.success(new PageInfo<>(listSubgroup));
    }

    /**
     * 员工查询
     *
     * @param pageParam 检索用参数
     * @return 实例对象
     * @author mwx
     * @since 2021年9月24日
     */
    @UserLoginToken
    @PostMapping(value = "/listUserInfo")
    @ApiOperation(httpMethod = "POST", value = "员工查询")
    public ResultBean<PageInfo<SubgroupUserInfoVo>> listUserInfo(@RequestBody PageParam<UserInfo> pageParam) {
        List<SubgroupUserInfoVo> listUserInfo = service.listUserInfo(pageParam);
        return ResultBean.success(new PageInfo<>(listUserInfo));
    }


    /**
     * 员工树   已弃用
     *
     * @return 实例对象
     * @author mwx
     * @since  2021年9月24日
     */
    @UserLoginToken
    @PostMapping(value = "/treeUserInfo")
    @ApiOperation(httpMethod = "POST", value = "全部员工树形数据")
    public ResultBean<PageInfo<TreeVo>> treeUserInfo() {
        List<TreeVo> treeUserInfo = service.treeUserInfo();
        return ResultBean.success(new PageInfo<>(treeUserInfo));
    }

    /**员工树
     *
     * @return 员工树
     */
    @UserLoginToken
    @PostMapping(value = "/groupEnterpriseGuid/treeUserInfo")
    @ApiOperation(httpMethod = "POST", value = "获取全部员工树形数据")
    public ResultBean<TreeVo> treeUserInfoByGroupEnterpriseGuid(@RequestBody SubgroupRequestBean subgroupRequestBean) {
        TreeVo treeVo = service.treeUserInfoByGroupEnterpriseGuid(subgroupRequestBean.getGroupEnterpriseGuid());
        return ResultBean.success(treeVo);
    }

    /**员工树  获取除登陆者之外的员工树，用于会议页面
     *
     * @return 除登陆者之外的员工树
     */
    @UserLoginToken
    @PostMapping(value = "/groupEnterpriseGuid/exceptLogin/treeUserInfo")
    @ApiOperation(httpMethod = "POST", value = "获取除登陆者之外的员工树")
    public ResultBean<TreeVo> treeUserInfoByGroupEnterpriseGuidExceptLogin(@RequestBody UserInfo userInfo) {
        TreeVo treeVo = service.treeUserInfoByGroupEnterpriseGuidExceptLogin(userInfo);
        return ResultBean.success(treeVo);
    }

    /**
     * 新增
     *
     * @param  subgroup ’实例对象‘
     * @return 1为成功
     * @author mwx
     * @since 2021年9月16日
     */
    @UserLoginToken
    @PostMapping(value = "/insert")
    @ApiOperation(httpMethod = "POST", value = "新增小组")
    public ResultBean<?> insert(@RequestBody @Valid SubgroupRequestBean subgroup, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBean.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        service.insertSubgroup(subgroup);
        return ResultBean.success();
    }

    /**
     * 分页查询
     *
     * @param pageParam 检索用参数
     * @return 实例对象
     * @author mwx
     * @since 2021年9月22日
     */
    @UserLoginToken
    @PostMapping(value = "/listSubgroupAndUserRelation")
    @ApiOperation(httpMethod = "POST", value = "小组成员")
    public ResultBean<PageInfo<SubgroupAndUserRelation>> listSubgroupAndUserRelation(
            @RequestBody @ApiParam(name = "") PageParam<SubgroupAndUserRelation> pageParam) {
        List<SubgroupAndUserRelation> listSubgroupAndUserRelation = service.listSubgroupAndUserRelation(pageParam);
        return ResultBean.success(new PageInfo<>(listSubgroupAndUserRelation));
    }



    /**
     * 修改
     *
     * @param subgroup ’实例对象‘
     * @return 1为成功
     * @author mwx
     * @since 2021年3月3日
     */
    @UserLoginToken
    @PutMapping(value = "/update")
    @ApiOperation(httpMethod = "PUT", value = "修改小组")
    public ResultBean<?> update(@RequestBody @Valid SubgroupRequestBean subgroup, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBean.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        service.updateSubgroup(subgroup);
        return ResultBean.success();
    }

    /**
     * 批量删除
     *
     * @param  subgroup ’id‘
     * @return 1为成功
     * @author mwx
     * @since 2021年9月16日
     */
    @UserLoginToken
    @DeleteMapping(value = "/deleteById")
    @ApiOperation(httpMethod = "DELETE", value = "批量删除")
    public ResultBean<?> deleteById(@RequestBody Subgroup subgroup) {
        List<String> ids = subgroup.getIds();
        service.deleteSubgroupById(ids);
        return ResultBean.success();
    }

}
